package com.kzw.oa.action.system;

import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.kzw.core.command.QueryFilter;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.comm.service.SystemLog2Service;
import com.kzw.oa.model.system.SystemLog;
import com.kzw.oa.service.system.SystemLogService;
import com.kzw.oa.util.JSON;



/**
 * 
 * @author 
 *
 */
public class SystemLogAction extends BaseAction{
	@Resource
	private SystemLogService systemLogService;
	@Autowired
	private SystemLog2Service systemLog2Service;
	
	private SystemLog systemLog;
	
	private Long logId;

	public Long getLogId() {
		return logId;
	}

	public void setLogId(Long logId) {
		this.logId = logId;
	}

	public SystemLog getSystemLog() {
		return systemLog;
	}

	public void setSystemLog(SystemLog systemLog) {
		this.systemLog = systemLog;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<SystemLog> list= systemLogService.getAll(filter);
		
		Type type=new TypeToken<List<SystemLog>>(){}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		
		//设置日期格式，不起作用？？
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		buff.append(gson.toJson(list, type));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	
	/**
	 * 显示列表（客户端分页）
	 */
	public String listAll(){		
	
		HttpServletRequest request = getRequest();
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		
		List<SystemLog> list = systemLog2Service.search(filters);		
		jsonString = new JSON(list).buildWithFilters(3);
		return SUCCESS;
	}
	
	/**
	 * 伪服务器端分页（一次性查出所有结果，但只返回部分值用于grid的显示）
	 */
	public String listAll2(){
		HttpServletRequest request = getRequest();
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		List<SystemLog> list = systemLog2Service.search(filters);
		
		// 构造分页信息
		PageRequest pageRequest = ExtjsUtil.getPageReq(getRequest());
		Page<SystemLog> page = new Page<SystemLog>(pageRequest);
		page.setTotalCounts(list.size());
		int endIndex =  page.getOffset()+page.getPageSize();
		if(endIndex > list.size()) {
			endIndex = list.size();
		}
		List<SystemLog> subList = list.subList(page.getOffset(), endIndex);
		page.setResult(subList);
		
		jsonString = new JSON(page).buildWithFilters(3);
		return SUCCESS;
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				systemLogService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		SystemLog systemLog=systemLogService.get(logId);
		
		Gson gson=new Gson();
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(systemLog));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		systemLogService.save(systemLog);
		setJsonString("{success:true}");
		return SUCCESS;
	}
}
